/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplepushserver.gui;


import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import simplepushserver.LogEntry;
import simplepushserver.SimpleLogger;

/**
 * @author Filip
 * The datamodel containing bicycledata, displayed using a table
 */
public class LogEntryTableModel extends AbstractTableModel {
    private final String[] columnNames = new String[]{"Time", "Source", "Action", "Content"};
    private final Class[] columnClasses = new Class[]{String.class, String.class, String.class, String.class};
    private final SimpleLogger log;
    public static final int TIME = 0;
    public static final int SOURCE = 1;
    public static final int ACTION = 2;
    public static final int CONTENT = 3;

    private final ChangeListener listerner = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Update!");
            fireTableStructureChanged();
            fireTableDataChanged();
        }
    };

    public LogEntryTableModel(SimpleLogger logger) {
        this.log = logger;
        this.log.addListener(listerner);
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return log.size();
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new RuntimeException("Not implemented");
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Class getColumnClass(int col) {
        return columnClasses[col];
    }

    @Override
    public Object getValueAt(int row, int col) {
        LogEntry b = log.getEntry(col);
        switch (col) {
            case TIME:
                return b.getTime();
            case SOURCE:
                return b.getSource();
            case ACTION:
                return b.getAction();
            case CONTENT:
                return b.getContent();
        }
        return "Error";
    }
}
